﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using ZBJF.Warranty.Common.Common;

namespace ZBJF.Warranty.WebProvider
{
    public class CookiesHelper
    {
        private CookiesHelper()
        {
        }

        private static CookiesHelper _instance;
        private static readonly object Lock = new object();
        public static CookiesHelper Instance
        {
            get { return _instance ?? (_instance = new CookiesHelper()); }
        }
        private string LoginSecurityKey = "wisetech";
        public void SetCookie(string key, string value, int expiresMinutes = 1440)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key] ?? new HttpCookie(key);
            cookie.Value = HttpUtility.UrlEncode(value);
            cookie.Expires = DateTime.Now.AddMinutes(expiresMinutes);
            HttpContext.Current.Response.Clear();//这里是关键，清除在返回前已经设置好的标头信息，这样后面的跳转才不会报错
            HttpContext.Current.Response.BufferOutput = true;//设置输出缓冲
            //服务器无法在发送 HTTP 标头时会对这些信息加密；此时修改cookie或输出缓存时将会出错；注：未知错误！
            //所以只要不同时跳转和修改cookie、输出缓存，就能解决问题；当然，也可以在webconfig配置中修改cacheRolesInCookie属性值设置成false就行了。 
            if (!HttpContext.Current.Response.IsRequestBeingRedirected)//在跳转之前做判断,防止重复
            {
                HttpContext.Current.Response.AppendCookie(cookie);
            }
        }

        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="decrypt"></param>
        /// <returns>cookie值</returns>
        public string GetCookie(string strName, bool decrypt=false)
        {
            if (HttpContext.Current!=null && HttpContext.Current.Request.Cookies[strName] != null)
            {
                if (decrypt)
                {
                    return
                        SecuritySignHelper.DESDecrypt(
                            HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[strName].Value), LoginSecurityKey);
                }
                return HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[strName].Value);
            }
            return string.Empty;
        }

        public void ClearCookies(string key)
        {
            var cookie = HttpContext.Current.Request.Cookies[key];
            if (cookie != null)
                HttpContext.Current.Response.Cookies.Remove(key);
            SetCookie(key, string.Empty, -1);
        }

        public void SetLoginSession(string userAccount)
        {
            SetCookie("UserAccount", SecuritySignHelper.DESEncrypt(userAccount, LoginSecurityKey));
        }

        public void SetLoginUserTypeSession(int userType)
        {
            SetCookie("BelongApplicationType", SecuritySignHelper.DESEncrypt(userType.ToString(), LoginSecurityKey));
        }
        public void ClearLoginSession()
        {
            ClearCookies("UserAccount");
            ClearCookies("BelongApplicationType");
        }
    }
}
